//
//  PaiGoodListViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/2/20.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "PaiGoodListViewController.h"
#import "PaiListViewCell.h"
#import "MMMarketPai.h"
#import "OYCountDownManager.h"
#import "PaiGoodDetailViewController.h"
#import "PublishGoodViewController.h"

@interface PaiGoodListViewController () <UITableViewDataSource, UITableViewDelegate, PaiListCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *pubButton;

@property (strong, nonatomic) NSMutableArray *nowArray;
@property (strong, nonatomic) NSMutableArray *comingArray;
@property (strong, nonatomic) NSMutableArray *finishArray;
@property (assign, nonatomic) NSInteger nowcount;
@property (assign, nonatomic) NSInteger comingcount;
@property (assign, nonatomic) NSInteger finishcount;

@end

@implementation PaiGoodListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"拍卖"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    self.nowcount = 0;
    self.comingcount = 0;
    self.finishcount = 0;
    [kCountDownManager start];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    [self loadNetworkData];
}

- (void)goPop {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)clickPai:(id)sender {
    PublishGoodViewController *vc = [[PublishGoodViewController alloc] initWithNibName:@"PublishGoodViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIView *)makeHeaderView:(NSString *)name {
    UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 70)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(70, 35, ScreenWidth - 140, 1)];
    line.backgroundColor = [UIColor blackColor];
    [view addSubview:line];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(120, 15, ScreenWidth - 140 - 100, 40)];
    title.text = name;
    title.font = [FontTool fontWithSize:15];
    title.backgroundColor = [UIColor whiteColor];
    title.textColor = [UIColor blackColor];
    title.textAlignment = NSTextAlignmentCenter;
    [view addSubview:title];
    
    title.layer.masksToBounds = YES;
    title.layer.cornerRadius = 4.f;
    [title.layer setBorderWidth:1];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace, (CGFloat[]){0.1, 0.1, 0.1, 1});
    [title.layer setBorderColor:colorref];
    CGColorRelease(colorref);
    CGColorSpaceRelease(colorSpace);
    
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1 || section == 3)
        return 10;
    else
        return 70;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0)
        return [self makeHeaderView:@"正在热拍"];
    else if (section == 1) {
        UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        view.backgroundColor = [UIColor colorWithHexString:@"E8E9F1"];
        return view;
    }
    else if (section == 2)
        return [self makeHeaderView:@"即将开始"];
    else if (section == 3) {
        UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        view.backgroundColor = [UIColor colorWithHexString:@"E8E9F1"];
        return view;
    }
    else
        return [self makeHeaderView:@"已经结束"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (ScreenWidth - 30 - 5) / 2 + 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
        return self.nowcount;
    else if (section == 1) {
        return 0;
    }
    else if (section == 2)
        return self.comingcount;
    else if (section == 3) {
        return 0;
    }
    else {
        return self.finishcount;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PaiListViewCell *cell = GET_CELL(PaiListViewCell, @"PaiListViewCell");
    MMMarketPai *ldata = nil;
    MMMarketPai *rdata = nil;
    if (indexPath.section == 0) {
        if (self.nowArray.count > indexPath.row * 2) {
            ldata = [self.nowArray objectAtIndex:(indexPath.row * 2)];
        }
        if (self.nowArray.count > indexPath.row * 2 + 1) {
            rdata = [self.nowArray objectAtIndex:(indexPath.row * 2 + 1)];
        }
    } else if (indexPath.section == 2) {
        if (self.comingArray.count > indexPath.row * 2) {
            ldata = [self.comingArray objectAtIndex:(indexPath.row * 2)];
        }
        if (self.comingArray.count > indexPath.row * 2 + 1) {
            rdata = [self.comingArray objectAtIndex:(indexPath.row * 2 + 1)];
        }
    }
    else if (indexPath.section == 4) {
        if (self.finishArray.count > indexPath.row * 2) {
            ldata = [self.finishArray objectAtIndex:(indexPath.row * 2)];
        }
        if (self.finishArray.count > indexPath.row * 2 + 1) {
            rdata = [self.finishArray objectAtIndex:(indexPath.row * 2 + 1)];
        }
    }
    [cell fillWithData:ldata right:rdata];
    cell.delegate = self;
    return cell;
}

- (void)clickPaiList:(MMMarketPai *)data {
    if(data != nil) {
        PaiGoodDetailViewController *vc = [[PaiGoodDetailViewController alloc] initWithNibName:@"PaiGoodDetailViewController" bundle:nil];
        vc.gooddata = data;
        vc.goodname = data.goods_name;
        vc.productId = data.goods_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)loadNetworkData {
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/product/productPMList", APIYIGOU];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSDictionary *data = [dict objectForKey:@"data"];
        [JSONModel addDatasFromDict:data withModel:@"MMMarketPai" forKey:@"nowPaimai" target:self.nowArray isAppend:NO];
        [JSONModel addDatasFromDict:data withModel:@"MMMarketPai" forKey:@"soonStartPaimai" target:self.comingArray isAppend:NO];
        [JSONModel addDatasFromDict:data withModel:@"MMMarketPai" forKey:@"finishPaimai" target:self.finishArray isAppend:NO];
        self.nowcount = self.nowArray.count / 2 + self.nowArray.count % 2;
        self.comingcount = self.comingArray.count / 2 + self.comingArray.count % 2;
        self.finishcount = self.finishArray.count / 2 + self.finishArray.count % 2;
        [self.tableView reloadData];
        [kCountDownManager reload];
    } error:^(YTKBaseRequest *request) {
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

ARRAY_INIT(nowArray)
ARRAY_INIT(comingArray)
ARRAY_INIT(finishArray)

@end
